package com.ruike.alisurface.ui.setting;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.utils.DeviceUtils;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: voodoo
 * CreateDate: 2020-04-16 016 下午 04:41
 * Description: 基础设置
 */
public class BasicSettingActivity extends BaseAty {

    @BindView(R.id.baseSetting_showAdv_swch)
    Switch showAdvSwch;
    @BindView(R.id.baseSetting_stopSell_swch)
    Switch stopSellSwch;
    @BindView(R.id.baseSetting_offlineRestart_swch)
    Switch offlineRestartSwch;

    @BindView(R.id.baseSetting_offlineRestart_et)
    EditText Restart_et;
    @BindView(R.id.baseSetting_offlineRestart_btn)
    Button Restart_btn;

    @BindView(R.id.baseSetting_questionCountdownTimer_et)
    EditText questionCountdownTimerEt;
    @BindView(R.id.baseSetting_saveQuestionCountdownTimer_btn)
    Button saveQuestionCountdownTimerBtn;
    @BindView(R.id.baseSetting_showAnswer_swch)
    Switch showAnswerSwch;
    @BindView(R.id.baseSetting_showAnswerTimer_et)
    EditText showAnswerTimerEt;
    @BindView(R.id.baseSetting_saveShowAnswerTimer_btn)
    Button saveShowAnswerTimerBtn;

    int purchaseCount; // 设置的购买数量

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_setting_base;
    }

    @Override
    protected void initViews() {
        initTitleBar(false);
    }

    int IS_SHOP_TYPE;

    @Override
    protected void initData() {
        // 购买数量
        purchaseCount = ShareUtils.getInstance().getInt(ShareKey.PURCHASE_COUNT, Constant.DefaultData.PURCHASE_COUNT);

        int isShowAdv = ShareUtils.getInstance().getInt(ShareKey.ADV_TYPE); // 展示界面广告
        showAdvSwch.setChecked(isShowAdv != Constant.ADV_TYPE_NONE); // 无广告为2，showAdvSwch状态为关

        boolean isStopSelling = ShareUtils.getInstance().getBoolean(ShareKey.IS_STOP_SELLING); // 停止售卖
        stopSellSwch.setChecked(isStopSelling);
        boolean isOfflineRestartApp = ShareUtils.getInstance().getBoolean(ShareKey.IS_OFFLINE_RESTART_APP); // 断网重启app
        offlineRestartSwch.setChecked(isOfflineRestartApp);
        IS_SHOP_TYPE = ShareUtils.getInstance().getInt(ShareKey.IS_SHOP_TYPE, 1); // 可商品类型展示

        int restCount = ShareUtils.getInstance().getInt(ShareKey.IS_OFFLINE_RESTART_APPCOUNT, 10);
        Restart_et.setText(String.valueOf(restCount));

        int questionShowTime = ShareUtils.getInstance().getInt(ShareKey.QUESTION_SHOW_TIME_KEY, 60); // 题目倒计时时长
        boolean isShowQuestionAnswer = ShareUtils.getInstance().getBoolean(ShareKey.IS_SHOW_QUESTION_ANSWER_KEY, false); // 保存是否展示正确答案
        int answerShowTime = ShareUtils.getInstance().getInt(ShareKey.ANSWER_SHOW_TIME_KEY, 2); // 展示正确答案时长
        questionCountdownTimerEt.setText(String.valueOf(questionShowTime));
        showAnswerSwch.setChecked(isShowQuestionAnswer);
        showAnswerTimerEt.setText(String.valueOf(answerShowTime));

    }

    @OnClick({R.id.baseSetting_goback_imgv, R.id.baseSetting_showAdv_swch, R.id.baseSetting_offlineRestart_btn,
            R.id.baseSetting_stopSell_swch, R.id.baseSetting_offlineRestart_swch,
            R.id.baseSetting_saveQuestionCountdownTimer_btn,
            R.id.baseSetting_showAnswer_swch,
            R.id.baseSetting_saveShowAnswerTimer_btn
    })
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.baseSetting_goback_imgv:
                if (stopSellSwch.isChecked()) {
                    startActivity(StopSellingActivity.class, null);
                } else {
                    finish();
                }
                break;
            case R.id.baseSetting_offlineRestart_btn:
                String Restart_etyStr = Restart_et.getText().toString().trim();
                if (!Restart_etyStr.isEmpty()) {
                    try {
                        int buyQuantity = Integer.parseInt(Restart_etyStr);
                        if (buyQuantity >= 0 && buyQuantity <= 100) {
                            ShareUtils.getInstance().putInt(ShareKey.IS_OFFLINE_RESTART_APPCOUNT, buyQuantity);
                            showRightTip(getResources().getString(R.string.setSuccessStr));
                        } else { // 小于0 或者大于3
                            showErrorTip(getResources().getString(R.string.setBuyCountErrorStr));
                            Restart_et.setText(buyQuantity <= 0 ? String.valueOf(1) : String.valueOf(10));
                        }
                    } catch (Exception e) {
                        showErrorTip("");
                    }
                } else {
                    showErrorTip("");
                }
                break;

            case R.id.baseSetting_showAdv_swch:
                ShareUtils.getInstance().putInt(ShareKey.ADV_TYPE, showAdvSwch.isChecked() ? Constant.ADV_TYPE_IMAGE : Constant.ADV_TYPE_NONE); // 选中的话为轮播图广告，关闭的话状态为2
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            case R.id.baseSetting_offlineRestart_swch:
                ShareUtils.getInstance().putBoolean(ShareKey.IS_OFFLINE_RESTART_APP, offlineRestartSwch.isChecked()); // 断网重启app
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            case R.id.baseSetting_saveQuestionCountdownTimer_btn:
                String questionCountdownTimerStr = questionCountdownTimerEt.getText().toString().trim();
                if (questionCountdownTimerStr.isEmpty()) {
                    showErrorTip("请输入到计时时长");
                    return;
                }
                int questionCountdownTimer = 0;
                try {
                    questionCountdownTimer = Integer.parseInt(questionCountdownTimerStr);
                } catch (Exception e) {
                    showErrorTip(getResources().getString(R.string.onlyInputIntNumStr));
                    return;
                }
                ShareUtils.getInstance().putInt(ShareKey.QUESTION_SHOW_TIME_KEY, questionCountdownTimer); // 题目倒计时时长
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            case R.id.baseSetting_showAnswer_swch:
                ShareUtils.getInstance().putBoolean(ShareKey.IS_SHOW_QUESTION_ANSWER_KEY, showAnswerSwch.isChecked()); // 保存是否展示正确答案
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            case R.id.baseSetting_saveShowAnswerTimer_btn:
                String showAnswerTimerStr = showAnswerTimerEt.getText().toString().trim();
                if (showAnswerTimerStr.isEmpty()) {
                    showErrorTip("请输入展示计时时长");
                    return;
                }
                int showAnswerTimer = 0;
                try {
                    showAnswerTimer = Integer.parseInt(showAnswerTimerStr);
                } catch (Exception e) {
                    showErrorTip(getResources().getString(R.string.onlyInputIntNumStr));
                    return;
                }
                ShareUtils.getInstance().putInt(ShareKey.ANSWER_SHOW_TIME_KEY, showAnswerTimer); // 展示正确答案时长
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            default:
                break;
        }
    }

    @Override
    public void doEventBusResult(MsgEventBus event) {

    }

}